
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.PlatformConfigDao;
import com.sun.showcase.biz.mall.service.PlatformConfigService;
import com.sun.showcase.client.domain.mall.PlatformConfig;
import com.sun.showcase.client.query.mall.PlatformConfigQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
@Service("platformConfigService")
@Transactional
public class PlatformConfigServiceImpl implements PlatformConfigService{
	@Autowired
	private PlatformConfigDao platformConfigDao;
	
	public void setPlatformConfigDao(PlatformConfigDao dao) {
		this.platformConfigDao = dao;
	}

	public DataGrid datagrid(PlatformConfigQuery platformConfigQuery) {
		DataGrid j = new DataGrid();
		Pager<PlatformConfig> pager  = find(platformConfigQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<PlatformConfigQuery> getQuerysFromEntitys(List<PlatformConfig> platformConfigs) {
		List<PlatformConfigQuery> platformConfigQuerys = new ArrayList<PlatformConfigQuery>();
		if (platformConfigs != null && platformConfigs.size() > 0) {
			for (PlatformConfig tb : platformConfigs) {
				PlatformConfigQuery b = new PlatformConfigQuery();
				BeanUtils.copyProperties(tb, b);
				platformConfigQuerys.add(b);
			}
		}
		return platformConfigQuerys;
	}

	private Pager<PlatformConfig> find(PlatformConfigQuery platformConfigQuery) {
		Page<PlatformConfig> page = PageHelper.startPage(platformConfigQuery.getPage().intValue(), platformConfigQuery.getRows().intValue());
		List<PlatformConfig> list = platformConfigDao.findList(platformConfigQuery);
		Pager<PlatformConfig> pager = new Pager<PlatformConfig>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public PlatformConfig add(PlatformConfigQuery platformConfigQuery) {
		PlatformConfig t = new PlatformConfig();
		BeanUtils.copyProperties(platformConfigQuery, t);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
		platformConfigDao.save(t);
		platformConfigQuery.setId(t.getId());
		return t;
		
	}

	public void update(PlatformConfigQuery platformConfigQuery) {
		PlatformConfig t = platformConfigDao.getById(platformConfigQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(platformConfigQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	platformConfigDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(PlatformConfigQuery platformConfigQuery) {
	    platformConfigDao.updatePart(platformConfigQuery);
	}

	public void delete(java.lang.Integer[] ids) {
		//platformConfigDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Integer[] ids) {
	//	platformConfigDao.deletePt(ids);
	}

	public PlatformConfig get(PlatformConfigQuery platformConfigQuery) {
		return platformConfigDao.getById(platformConfigQuery.getId());
	}

	public PlatformConfig get() {
		return platformConfigDao.getById(1);
	}

	
	public List<PlatformConfigQuery> listAll(PlatformConfigQuery platformConfigQuery) {
	    List<PlatformConfig> list = platformConfigDao.findList(platformConfigQuery);
		List<PlatformConfigQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
